package me.pplay.playtalk.view;

import java.util.ArrayList;
import java.util.List;

import me.pplay.playtalk.R;
import me.pplay.playtalk.adapter.MyPagerAdapter;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class CommonTabTopLayout extends FrameLayout implements OnCheckedChangeListener{

	public static final int SETADAPTER 	= 200;
	private Context mContext;
	private HorizontalScrollView mHorizontalScrollView;
	private float mCurrentCheckedRadioLeft;
	private RadioGroup mRadioGroup;
	private ImageView mImageView;
	private ViewPager mViewPager;
	private List<View> mViews;
	private int width = 0;
	private int currentIndex = 0;
	private String[] mTotalColum;
	private Handler mHandler;
	private Message msg;
	public List<View> backViews = new ArrayList<View>();
	private MyPagerAdapter pagerAdapter;
	
	public void addView(List<View> views){
//		backViews.add(view);
		mViews = views;
		pagerAdapter = null;
		pagerAdapter = new MyPagerAdapter(mViews);
		mViewPager.setAdapter(pagerAdapter);
//		pagerAdapter.notifyDataSetChanged();
	}
	
	public CommonTabTopLayout(Context context,AttributeSet attrs) {
		super(context, attrs);
		mContext		= context;
	}
	
	public void setTabTopLayout(int width, String[] totalColumn, List<View> views, Handler handler){
		this.width		= width;
		mTotalColum		= totalColumn;	
		mViews			= views;
		mHandler		= handler;
		addView();
	}
	
	private void addView(){
		View v					= LayoutInflater.from(mContext).inflate(R.layout.layout_tab_top, null);
		mHorizontalScrollView	= (HorizontalScrollView)v.findViewById(R.id.horizontalScrollView);
		mRadioGroup				= (RadioGroup)v.findViewById(R.id.radioGroup);
		mViewPager				= (ViewPager)v.findViewById(R.id.pager);
		mImageView				= (ImageView)v.findViewById(R.id.img1);
		this.addView(v, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
		if(mViews == null)
			mViewPager.setVisibility(View.GONE);
		iniController();
		iniListener();
	}
	
	private void iniController(){
		int count 		= mTotalColum.length;
		for(int i = 0; i < count; i++){
			RadioButton rdo = new RadioButton(mContext);
			rdo.setText(mTotalColum[i]);
			rdo.setId(i);
			setRadioGroupStyle(rdo);
			rdo.setOnClickListener(new RedioButtonClickListener(i));
			mRadioGroup.addView(rdo);
		}
		pagerAdapter = new MyPagerAdapter(mViews);
		mViewPager.setAdapter(pagerAdapter);
	}

	private void iniListener() {
		mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				currentIndex = checkedId;
				setHandler();
			}
		});
		mRadioGroup.setOnCheckedChangeListener(this);
		if(mViews == null)
			return;
		mViewPager.setOnPageChangeListener(new MyPagerOnPageChangeListener());
	}
	

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		currentIndex = checkedId;
		AnimationSet _AnimationSet = new AnimationSet(true);
		TranslateAnimation _TranslateAnimation;
		_TranslateAnimation = new TranslateAnimation(mCurrentCheckedRadioLeft, (float)(checkedId*(width)), 0f, 0f);
		_AnimationSet.addAnimation(_TranslateAnimation);
		_AnimationSet.setFillBefore(true);
		_AnimationSet.setFillAfter(true);
		_AnimationSet.setDuration(200);
		mImageView.startAnimation(_AnimationSet);
		if(mViews != null)
			mViewPager.setCurrentItem(checkedId);
		mCurrentCheckedRadioLeft = (mRadioGroup.getCheckedRadioButtonId())*(width);
		mHorizontalScrollView.smoothScrollTo((int)mCurrentCheckedRadioLeft-(int)(width), 0); 
	}
	
	private void setHandler(){
		if(mHandler == null)
			return;
//		msg.recycle();
		msg			= null;
		msg			= new Message();
		msg.what	= SETADAPTER;
		msg.arg1	= currentIndex;
		mHandler.sendMessage(msg);
	}
	

	private class RedioButtonClickListener implements OnClickListener{
		private int index = 0;
		public RedioButtonClickListener(int position){
			index = position;
		}
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			currentIndex = index;
			setHandler();
		}
		
	}
	
	private class MyPagerOnPageChangeListener implements OnPageChangeListener{

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}
		
		@Override
		public void onPageSelected(int position) {
			currentIndex = position;
			Log.i("zj", "position="+position);
			if(mViews.size() > position){
				setHandler();
			}
			mViewPager.setCurrentItem(position);
			mRadioGroup.getChildAt(position).performClick();
		}
		
	}
	
	private void setRadioGroupStyle(RadioButton rdo){
		 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, LayoutParams.FILL_PARENT);
		 params.weight	= (float)1.0;
		 rdo.setLayoutParams(params);
		 rdo.setPadding(10, 10, 10, 10);
		 rdo.setGravity(Gravity.CENTER);
		 rdo.setButtonDrawable(null);
		 rdo.setEllipsize(TruncateAt.MARQUEE);
		 rdo.setSingleLine(true);
		 rdo.setBackgroundResource(R.drawable.radiobtn_selector);
	}
	
	public void setBackView(View view){
		backViews.add(view);
	}
	
	public void clearBackView(){
		backViews.clear();
	}
}
